package com.ch999.mobileoa.page;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.ch999.mobileoa.HomeActivity;
import com.ch999.mobileoa.R;
import com.sda.lib.util.Tools;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by scorpio on 15/12/25.
 */
public class LoadingActivity extends Activity implements View.OnClickListener {

    @ViewInject(id = R.id.view_permission)
    View view_permission;
    @ViewInject(id = R.id.btn_ok, click = "onClick")
    Button btn_ok;

    Context context ;
    /**
     * APP 需要获取的权限控制
     */
    String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION
            , Manifest.permission.ACCESS_COARSE_LOCATION
            , Manifest.permission.READ_PHONE_STATE
            , Manifest.permission.CAMERA
            , Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        FinalActivity.initInjectedView(this);
        context = LoadingActivity.this ;
        initView();

    }

    /**
     * 6.0 的手机进行权限申请
     */
    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Tools.checkoutPermissions(context, PERMISSIONS)) {
                view_permission.setVisibility(View.VISIBLE);
            }else {
                view_permission.setVisibility(View.GONE);
                nextPage();
            }
        } else {
            view_permission.setVisibility(View.GONE);
            nextPage();
        }
    }



    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_ok:
                // 开始请求权限
                Tools.requestPermissions(this, PERMISSIONS);
                break;
        }
    }


    /**
     * 监听权限 申请结果
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        int count = 0 ;
        for (int i :grantResults){
            if(i == 0){
                count = count +1 ;
            }
        }
        if (count == PERMISSIONS.length){
            view_permission.setVisibility(View.GONE);
            nextPage();
        }else{
            Tools.checkoutPermissions(context,permissions);
        }
    }

    private void nextPage() {
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(context, HomeActivity.class));
                finish();
            }
        };
        timer.schedule(timerTask,1000);
    }
}
